//package com.it.water.controller.VideoController;
//
//import org.bytedeco.javacv.Frame;
//import org.bytedeco.javacv.FFmpegFrameGrabber;
//import org.bytedeco.javacv.Java2DFrameConverter;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
//import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
//
//import javax.imageio.ImageIO;
//import javax.servlet.http.HttpServletResponse;
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayOutputStream;
//
//@RestController
//public class VideoStreamController {
//
//    @GetMapping("/video/stream")
//    public ResponseBodyEmitter streamVideo(HttpServletResponse response) {
//        ResponseBodyEmitter emitter = new ResponseBodyEmitter();
//        Thread thread = new Thread(() -> {
//            try {
//                String rtspUrl = "rtsp://admin:admin@192.168.137.80:8554/live"; // 替换为实际的移动设备RTSP流地址
//                FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl);
//                grabber.start();
//                Java2DFrameConverter converter = new Java2DFrameConverter();
//
//                while (true) {
//                    Frame frame = grabber.grabImage();
//                    if (frame != null) {
//                        BufferedImage bufferedImage = converter.getBufferedImage(frame, 1.0);
//                        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//                        ImageIO.write(bufferedImage, "jpg", outputStream);
//                        emitter.send(outputStream.toByteArray());
//                    } else {
//                        System.out.println("无法读取帧");
//                        break;
//                    }
//                }
//
//                grabber.stop();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
//        thread.start();
//        return emitter;
//    }
//
//    @GetMapping("/video/streamWebSocket")
//    public SseEmitter streamVideoWebSocket() {
//        SseEmitter emitter = new SseEmitter();
//        Thread thread = new Thread(() -> {
//            try {
//                String rtspUrl = "rtsp://admin:admin@192.168.137.80:8554/live"; // 替换为实际的移动设备RTSP流地址
//                FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl);
//                grabber.start();
//                Java2DFrameConverter converter = new Java2DFrameConverter();
//
//                while (true) {
//                    Frame frame = grabber.grabImage();
//                    if (frame != null) {
//                        BufferedImage bufferedImage = converter.getBufferedImage(frame, 1.0);
//                        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//                        ImageIO.write(bufferedImage, "jpg", outputStream);
//                        emitter.send(outputStream.toByteArray());
//                    } else {
//                        System.out.println("无法读取帧");
//                        break;
//                    }
//                }
//
//                grabber.stop();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
//        thread.start();
//        return emitter;
//    }
//}
